﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace WebSource
{
    public class WebSource
    {
        #region GetJPGfromWeb
        public static string[] GetJPGfromWeb(string src)
        {
            int i = 0;
            List<string> arrStr = new List<string>();
            MatchCollection mc = Regex.Matches(src, @"[a-zA-z]+://[^\s]*.jpg");
            if (mc.Count < 1) return null;
            for (i = 0; i < mc.Count; i++)
            {
                arrStr.Add(mc[i].Value);
            }
            return arrStr.ToArray();
        }
        public static  string[] GetJPGfromWeb(string src, string SiteURLFilter)
        {
            int i = 0;
            List<string> arrStr = new List<string>();
            MatchCollection mc = Regex.Matches(src, @"[a-zA-z]+://[^\s]*" + SiteURLFilter + @"[^\s]*.jpg");
            if (mc.Count < 1) return null;
            for (i = 0; i < mc.Count; i++)
            {
                arrStr.Add(mc[i].Value);
            }
            return arrStr.ToArray();
        }
        #endregion

        public static  string[] GetLinksFromWeb(string src, string StartFlag)
        {
            int i = 0;
            List<string> arrStr = new List<string>();
            MatchCollection mc = Regex.Matches(src, StartFlag + @"href=[^\s]*</a>");
            for (i = 0; i < mc.Count; i++)
            {
                arrStr.Add(mc[i].Value);
            }
            return arrStr.ToArray();
        }

        #region --------Get URLs---------------
        public static string[] GetURLs(string src)
        {
            int i = 0;
            List<string> arrStr = new List<string>();
            MatchCollection mc = Regex.Matches(src, @"[a-zA-z]+://[^\s]*"); //Regex for http://  ready to be used
            for (i = 0; i < mc.Count; i++)
            {
                arrStr.Add(mc[i].Value);
            }
            return arrStr.ToArray();
        }
        public static string GetURLs(string src,int index)
        {
            return GetURLs (src)[index];
        }
        #endregion

        public static string GetPageMode(string src, string StartFlag)
        {
            int i = 0;
            StringBuilder strTmp = new StringBuilder();
            List<string> arrStr = new List<string>();

            if (src == "") return "";
            //MatchCollection mc = Regex.Matches(src, StartFlag + @"(?<=typelink).*");
            //MatchCollection mc = Regex.Matches(src, StartFlag + @"(?<=typelink).*\x3c\/a\x3e\x3c\/h2");
            MatchCollection mc = Regex.Matches(src, StartFlag + @"(?<=typelink).*\x3c\/a\x3e\x3c\/h2");
            for (i = 0; i < mc.Count; i++)
            {
                arrStr.Add(mc[i].Value);
            }
            if (i > 0)
            {
                strTmp.Append(arrStr.ToArray()[0]);
                strTmp.Remove(0, 3);
                strTmp.Remove(strTmp.Length - 8, 8);
                return strTmp.ToString();
            }
            else return "";

        }

        public static string GetPageTitle(string src, string StartFlag)
        {
            int i = 0;
            StringBuilder strTmp = new StringBuilder();
            List<string> arrStr = new List<string>();
            //MatchCollection mc = Regex.Matches(src, StartFlag + @"(?<=typelink).*");
            //MatchCollection mc = Regex.Matches(src, StartFlag + @"(?<=typelink).*\x3c\/a\x3e\x3c\/h2");
            if (src == "") return "";
            MatchCollection mc = Regex.Matches(src, StartFlag + @"(<title>).*(<\/title>)");
            for (i = 0; i < mc.Count; i++)
            {
                arrStr.Add(mc[i].Value);
            }
            strTmp.Append(arrStr.ToArray()[0]);
            strTmp.Remove(0, 7);
            //strTmp.Remove(strTmp.Length - 22, 22);
            return strTmp.ToString();
        }

        public static string ReadPageSource(string filepath)
        {
            string str = "";
            System.IO.StreamReader sr = new StreamReader(filepath, Encoding.GetEncoding("GBK"));
            str = sr.ReadToEnd();
            sr.Close();
            return str;
        }
    }
}
